package com.cnjson.sensor.protocolARP.waterQualitySensorProtocol.response;

import com.cnjson.sensor.util.CRC16;

import java.nio.ByteBuffer;

public class WaterQualitySensorResponse {


    /**
     * 构建应答设备登录（0x41）的数据包
     * 注册成功：0x01 错误标志：0xCF 0xCF 0x01：未配置 0x02：数据内容错误
     *
     * @param device 4位地址
     * @return 完整的数据
     */
    public static byte[] buildRegisterPackage(byte[] device, boolean exist) {

        byte[] content;
        if (exist) {
            content = new byte[]{0x01};
        } else {
            content = new byte[]{(byte) 0xCF, (byte) 0xCF, 0x01};
        }
        return buildResponsePackage((byte) 0x41, device, content);
    }

    /**
     * 构建应答数据包。
     *
     * @param direction 标识位，即功能代码
     * @param device    设备地址字节数组
     * @param content   要应答数据内容
     * @return
     */
    public static byte[] buildResponsePackage(byte direction, byte[] device, byte[] content) {
        // 响应数据包总长度=[帧头2] +[方向1]+[帧长度位1]＋[帧代号1]+[功能码2]+ [地址段4] + [数据n]
        // +[CRC校验2]+[包尾2]
        final int dlen = device.length;
        final int len = 7 + dlen + content.length + 2 + 2;
        byte[] response = new byte[len];
        response[0] = (byte) 0x88;
        response[1] = (byte) 0xFB;
        response[2] = (byte) 0xAF;
        // 长度值 = [帧代号1]+[功能码2] + [地址段m] + [数据n] +[CRC校验2]
        response[3] = (byte) (len - 6);// 数据包有效长度值，从长度位到负载的
        response[4] = direction;
        response[5] = (byte) 0x61;
        response[6] = (byte) 0x00;
        System.arraycopy(device, 0, response, 7, dlen);// 设备在址
        System.arraycopy(content, 0, response, 7 + dlen, content.length);// 应答内容
        byte[] resCrc = new byte[len - 7];
        System.arraycopy(response, 3, resCrc, 0, len - 7);
        byte[] nR = CRC16.getCrcBytes(resCrc);
        System.arraycopy(nR, 0, response, len - 4, 2);
        // 包尾
        response[len - 2] = (byte) 0xFC;
        response[len - 1] = (byte) 0xFC;
        return response;
    }



    /**
     * @param direction 标识位，即功能代码
     * @param device    设备地址字节数组
     * @param content   要应答数据内容
     * @return
     */
    public static byte[] buildRequestPackage(byte direction, byte[] device, byte[] content) {

        // 请求数据包总长度=[帧头2] +[方向1]+[帧长度位1]＋[帧代号1]+[功能码2]+ [地址段4] + [数据n]
        // +[CRC校验2]+[包尾2]
        final int dlen = device.length;
        final int len = 7 + dlen + content.length + 2 + 2;

        byte[] request = new byte[len];
        request[0] = (byte) 0x88;
        request[1] = (byte) 0xFB;
        request[2] = (byte) 0xFA;

        // 长度值 = [帧代号1]+[功能码2] + [地址段m] + [数据n] +[CRC校验2]
        request[3] = (byte) (len - 6);// 数据包有效长度值，从长度位到负载的
        request[4] = direction;
        request[5] = (byte) 0x41;
        request[6] = (byte) 0x01;
        System.arraycopy(device, 0, request, 7, dlen);// 设备地址
        System.arraycopy(content, 0, request, 7 + dlen, content.length);// 应答内容
        byte[] resCrc = new byte[len - 7];
        System.arraycopy(request, 3, resCrc, 0, len - 7);
        byte[] nR = CRC16.getCrcBytes(resCrc);
        System.arraycopy(nR, 0, request, len - 4, 2);

        // 包尾
        request[len - 2] = (byte) 0xFC;
        request[len - 1] = (byte) 0xFC;
        return request;
    }


}
